package com.puboot.module.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.puboot.common.util.Pagination;
import com.puboot.component.FreeTranslateService;
import com.puboot.module.model.LkChapter;
import com.puboot.module.model.LkComic;
import com.puboot.module.mapper.LkComicMapper;
import com.puboot.module.model.LkComicLang;
import com.puboot.module.service.LkComicLangService;
import com.puboot.module.service.LkComicService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;

import java.util.List;


/**
 * (LkComic)表服务实现类
 *
 * @author Leonidax
 * @since 2024-10-11 16:09:04
 */
@Service
@AllArgsConstructor
public class LkComicServiceImpl extends ServiceImpl<LkComicMapper,LkComic> implements LkComicService {
    private final int pageSize = 20;
    private final LkComicMapper mapper;
    private final FreeTranslateService freeTranslateService;
    private final LkComicLangService comicLangService;

    private void translateList(List<LkComic> list, String lang){
        for(int i=0; i<list.size(); i++){
            if(list.get(i).getTitle() == null){
                LkComic comic = list.get(i);
                LkComicLang comicLang = this.translate(comic.getId(),lang);
                comic.setTitle(comicLang.getTitle());
                comic.setIntro(comicLang.getIntro());
            }
        }
    }


    @Override
    public IPage<LkComic> check(Integer page) {
        IPage<LkComic> iPage = new Pagination<>(page,100);
        return mapper.check(iPage);
    }

    @Override
    public List<LkComic> limitIsFee(Boolean isFee) {
        return mapper.limitIsFee(isFee);
    }

    @Override
    public int updateIsFee(Integer id) {
        return mapper.updateIsFee(id);
    }

    @Override
    public List<LkComic> limitTotal(Integer total, Integer to) {
        return mapper.limitTotal(total, to);
    }
    @Override
    public List<LkComic> limitTotal(Integer total) {
        return mapper.limitTotal(total, 100);
    }

    @Override
    public int updateTotal(Integer id) {
        return mapper.updateTotal(id);
    }

    @Override
    public List<LkComic> search(String word, String lang) {
        return mapper.search(word,lang);
    }

    @Override
    public LkComicLang translate(Integer id, String lang) {
        LkComic comic = this.getById(id);
        String title = freeTranslateService.translate(comic.getTitle(),lang);
        String intro = freeTranslateService.translate(comic.getIntro(),lang);
        LkComicLang comicLang = new LkComicLang();
        comicLang.setComicId(comic.getId());
        comicLang.setLang(lang);
        comicLang.setTitle(title);
        comicLang.setIntro(intro);
        if(title == null) return comicLang;
        comicLangService.save(comicLang);
        return comicLang;
    }

    @Override
    @Cacheable(value = "random", key = "#type+ ':' + #size+':'+ #lang")
    public List<LkComic> random(Integer type, Integer size, String lang) {
        List<LkComic> list = mapper.random(size,lang);
        translateList(list,lang);
        return list;
    }

    @Override
    @Cacheable(value = "rank", key = "#page+':'+ #lang")
    public IPage<LkComic> rank(Integer page, String lang) {
        IPage<LkComic> iPage = new Pagination<>(page,pageSize);
        iPage = mapper.rank(lang, iPage);
        List<LkComic> list = iPage.getRecords();
        translateList(list,lang);
        return iPage;
    }

    @Override
    @Cacheable(value = "all", key = "#page+':'+ #lang")
    public IPage<LkComic> all(Integer page, String lang) {
        IPage<LkComic> iPage = new Pagination<>(page,pageSize);
        iPage = mapper.all(lang, iPage);
        List<LkComic> list = iPage.getRecords();
        translateList(list,lang);
        return iPage;
    }

    @Override
    @Cacheable(value = "cate", key = "#cate+ ':' + #page+':'+ #lang")
    public IPage<LkComic> cate(Integer cate, Integer page, String lang) {
        IPage<LkComic> iPage = new Pagination<>(page,pageSize);
        iPage = mapper.cate(cate, lang, iPage);
        List<LkComic> list = iPage.getRecords();
        translateList(list,lang);
        return iPage;
    }

    @Override
    public LkComic queryById(Integer id, String lang) {
        return mapper.queryById(id, lang);
        /**if(comic.getTitle() != null) return comic;
        LkComicLang comicLang = this.translate(comic.getId(),lang);
        comic.setTitle(comicLang.getTitle());
        comic.setIntro(comicLang.getIntro());
        return comic;*/
    }

    /**
     * 分页查询
     *
     * @param lkComic 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<LkComic> queryByPage(LkComic lkComic, PageRequest pageRequest) {
        long total = mapper.count(lkComic);
        return new PageImpl<>(mapper.queryAllByLimit(lkComic, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param lkComic 实例对象
     * @return 实例对象
     */
    @Override
    public LkComic insert(LkComic lkComic) {
        this.mapper.insert(lkComic);
        return lkComic;
    }

    /**
     * 修改数据
     *
     * @param lkComic 实例对象
     * @return 实例对象
     */
    @Override
    public LkComic update(LkComic lkComic) {
        this.mapper.update(lkComic);
        return null;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.mapper.deleteById(id) > 0;
    }
}
